<?php

namespace app\admin\model\trades\assets;

use app\admin\model\cms\ChannelAdmin;
use think\Config;
use think\Model;
use think\Db;

class Card extends Model
{

    
    const IS_DEFAULT_1 = 1;//默认绑定账户
    const IS_DEFAULT_0 = 0;//不是默认绑定账户
    const IS_SUCCESS_0 = 0;//没绑定
    const IS_SUCCESS_1 = 1;//绑定成功


    // 表名
    protected $table = 'account_card';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        'is_success_text',
        'is_default_text',
    ];


//    //绑定成功
    public function getIsSuccessList()
    {
        return ['0' => __('is_success 0'),'1' => __('is_success 1')];
    }
    //是否为默认绑定卡
    public function getIsDefaultList()
    {
        return ['0' => __('is_default 0'),'1' => __('is_default 1')];
    }

    //获取是否绑定成功字段
    public function getIsSuccessTextAttr($value, $data)
    {
        $value = $value ? $value : $data['is_success'];
        $list = $this->getIsSuccessList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    //获取默认绑定卡字段
    public function getIsDefaultTextAttr($value, $data)
    {
        $value = $value ? $value : $data['is_default'];
        $list = $this->getIsDefaultList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    /*
     * 获取卡类型
     */
    public function getCardList(){
        $list = Config::get('bank_card');
        $data = [];
        if($list){
            foreach($list as $k=>$v){
                    $data[$v['id']] = $v['name'];
            }
        }
        unset($list);
        return $data;
    }

    /*
     *查找某用户是否设置了绑定提现账户
     */
    public function  getCardNumByid($id,$account_id){
        $where['account_id'] = $account_id;
        $where['is_default'] = self::IS_DEFAULT_1;
        $list = $this->getModel()->where($where)->find();
        if($list){
            return true;
        }else{
            return false;
        }

    }

    /*
     * $card_type 卡类型
     */
    public function getCardTypeNameById($card_type){
        if(empty($card_type))
            return false;
        $list = Config::get('bank_card');
        foreach($list as $k=>$v){
            if($card_type == $v['id'])
                return $v['name'];
        }

        return false;
    }

    /*
     * 查找是否绑定卡并且设置提现卡
     */
    public static function getCardInfoById($account_id){
        if(empty($account_id))
            return false;
        $where['is_default'] = 1;
        $where['is_success'] = 1;
        $res = Db::name("account_card")->where('account_id', $account_id)->where($where)->find();
        if($res)
            return $res;
        else
            return '';
    }
}
